/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client;

import java.io.IOException;
import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.TargetListener;
import javax.microedition.contactless.TargetType;
import javax.microedition.contactless.ndef.NDEFRecordListener;
import javax.microedition.contactless.ndef.NDEFRecordType;

/**
 *
 * @author Marnix Renne
 */
public class ContactlessUtils {

    /**
     * Discover supported targets, registers listeners
     *
     * @param targetListener the target listener
     */
    public static void registerTargetListeners(TargetListener targetListener) throws ContactlessException {
        // Discover supported types
        DiscoveryManager discoveryManager = DiscoveryManager.getInstance();
        TargetType[] tp = DiscoveryManager.getSupportedTargetTypes();
        // Register listener for each of the supported types
        for (int i = 0; i < tp.length; i++) {
            if (tp[i].equals((TargetType.ISO14443_CARD))) {
                discoveryManager.addTargetListener(
                        targetListener, TargetType.ISO14443_CARD);
            } else if (tp[i].equals(TargetType.NDEF_TAG)) {
                discoveryManager.addTargetListener(
                        targetListener, TargetType.NDEF_TAG);
            } else if (tp[i].equals(TargetType.RFID_TAG)) {
                discoveryManager.addTargetListener(
                        targetListener, TargetType.RFID_TAG);
            } else if (tp[i].equals(TargetType.VISUAL_TAG)) {
                discoveryManager.addTargetListener(
                        targetListener, TargetType.VISUAL_TAG);
            }
        }
    }

    public static void unregisterTargetListeners(TargetListener targetListener) {
        // Discover supported types
        DiscoveryManager discoveryManager = DiscoveryManager.getInstance();
        TargetType[] tp = DiscoveryManager.getSupportedTargetTypes();
        // Register listener for each of the supported types
        for (int i = 0; i < tp.length; i++) {
            if (tp[i].equals((TargetType.ISO14443_CARD))) {
                discoveryManager.removeTargetListener(
                        targetListener, TargetType.ISO14443_CARD);
            } else if (tp[i].equals(TargetType.NDEF_TAG)) {
                discoveryManager.removeTargetListener(
                        targetListener, TargetType.NDEF_TAG);
            } else if (tp[i].equals(TargetType.RFID_TAG)) {
                discoveryManager.removeTargetListener(
                        targetListener, TargetType.RFID_TAG);
            } else if (tp[i].equals(TargetType.VISUAL_TAG)) {
                discoveryManager.removeTargetListener(
                        targetListener, TargetType.VISUAL_TAG);
            }
        }
    }
    
    public static void registerNDEFRecordListeners(NDEFRecordListener ndefRecordListener) throws ContactlessException {
        DiscoveryManager discoveryManager = DiscoveryManager.getInstance();
        NDEFRecordType recordType = new NDEFRecordType(NDEFRecordType.EMPTY, null);
        discoveryManager.addNDEFRecordListener(ndefRecordListener, recordType);

        recordType = new NDEFRecordType(NDEFRecordType.UNKNOWN, null);
        discoveryManager.addNDEFRecordListener(ndefRecordListener, recordType);

        recordType = new NDEFRecordType(NDEFRecordType.MIME, "text/x-vCard");
        discoveryManager.addNDEFRecordListener(ndefRecordListener, recordType);

        recordType = new NDEFRecordType(NDEFRecordType.MIME, "text/x-vCalendar");
        discoveryManager.addNDEFRecordListener(ndefRecordListener, recordType);

        // Smart posters
        recordType = new NDEFRecordType(NDEFRecordType.NFC_FORUM_RTD, "urn:nfc:wkt:Sp");
        discoveryManager.addNDEFRecordListener(ndefRecordListener, recordType);

        // URI records
        recordType = new NDEFRecordType(NDEFRecordType.NFC_FORUM_RTD, "urn:nfc:wkt:U");
        discoveryManager.addNDEFRecordListener(ndefRecordListener, recordType);
    }
    
    public static void unregisterNDEFRecordListeners(NDEFRecordListener ndefRecordListener) {
        DiscoveryManager discoveryManager = DiscoveryManager.getInstance();
        NDEFRecordType recordType = new NDEFRecordType(NDEFRecordType.EMPTY, null);
        discoveryManager.removeNDEFRecordListener(ndefRecordListener, recordType);

        recordType = new NDEFRecordType(NDEFRecordType.UNKNOWN, null);
        discoveryManager.removeNDEFRecordListener(ndefRecordListener, recordType);

        recordType = new NDEFRecordType(NDEFRecordType.MIME, "text/x-vCard");
        discoveryManager.removeNDEFRecordListener(ndefRecordListener, recordType);

        recordType = new NDEFRecordType(NDEFRecordType.MIME, "text/x-vCalendar");
        discoveryManager.removeNDEFRecordListener(ndefRecordListener, recordType);

        // Smart posters
        recordType = new NDEFRecordType(NDEFRecordType.NFC_FORUM_RTD, "urn:nfc:wkt:Sp");
        discoveryManager.removeNDEFRecordListener(ndefRecordListener, recordType);

        // URI records
        recordType = new NDEFRecordType(NDEFRecordType.NFC_FORUM_RTD, "urn:nfc:wkt:U");
        discoveryManager.removeNDEFRecordListener(ndefRecordListener, recordType);        
    }
}
